﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using StuffNET.Forms;
using StuffNET.Math;
using StuffNET.Other;
using StuffNET.Physics;

namespace UnitTests
{
    public partial class UnitTestsForm : Form
    {
        ProgressForm pf;
        InputListener il = new InputListener();
        TestCollisionObject tco1, tco2, tco3, tco4;
        CollisionDetection cd = new CollisionDetection();

        public UnitTestsForm()
        {
            InitializeComponent();

            tco3 = new TestCollisionObject(new Coordinate(5, 60), new Coordinate(35, 30), new Coordinate(65, 60), new Coordinate(35, 90), new Coordinate(40, 75), new Coordinate(35, 60));
            tco2 = new TestCollisionObject(CollisionDetection.GetRectangle(new Coordinate(150, 10), new Coordinate(170, 150)));
            tco1 = new TestCollisionObject(CollisionDetection.GetCircle(new Coordinate(50, 80), 41, 120));
            tco4 = new TestCollisionObject(CollisionDetection.GetRectangle(new Coordinate(0, 0), new Coordinate(collisionDBPanel.Width, collisionDBPanel.Height)));

            cd.AddObject(tco1);
            cd.AddObject(tco2);
            cd.AddObject(tco3);
            cd.AddObject(tco4);
            cd.Start();

            simTimer.Tick += CollisionDetection_Tick;
            il.Start();
        }


        private void progressBarButton_Click(object sender, EventArgs e)
        {
            pf = new ProgressForm("Test1", ProgressBarStyle.Blocks,0);
            pf.CurrentOperationInfo = "Initialze...";
            simTimer.Tick += simTimer_progressBarTick;
        }
        void simTimer_progressBarTick(object sender, EventArgs e)
        {
            pf.Value++;
            if (pf.Value == 24)
                pf.CurrentOperationInfo = "Comparing...";
            if (pf.Value == 61)
                pf.CurrentOperationInfo = "Writing new Values...";
            if (pf.Value == 91)
                pf.CurrentOperationInfo = "Finilize...";

            if (!pf.IsOpen)
                simTimer.Tick -= simTimer_progressBarTick;
        }
        private void executeButton_Click(object sender, EventArgs e)
        {
            object result = StuffNET.Other.DynamicCompiler.Execute(codeTextBox.Text, new string[] { "System.Windows.Forms.dll" }, new string[] { "System.Windows.Forms", "System" });
            if (result != null)
            {
                MessageBox.Show(result.ToString());
            }
        }

        private void CollisionDetection_Tick(object sender, EventArgs e)
        {
            if (il.PressedKeys.Contains(Keys.Left))
            {
                tco1.Offset(-1.5, 0);
            }
            if (il.PressedKeys.Contains(Keys.Right))
            {
                tco1.Offset(1.5, 0);
            }
            if (il.PressedKeys.Contains(Keys.Up))
            {
                tco1.Offset(0, -1.5);
            }
            if (il.PressedKeys.Contains(Keys.Down))
            {
                tco1.Offset(0, 1.5);
            }

            if (il.PressedKeys.Contains(Keys.A))
            {
                tco3.Offset(-1.5, 0);
            }
            if (il.PressedKeys.Contains(Keys.D))
            {
                tco3.Offset(1.5, 0);
            }
            if (il.PressedKeys.Contains(Keys.W))
            {
                tco3.Offset(0, -1.5);
            }
            if (il.PressedKeys.Contains(Keys.S))
            {
                tco3.Offset(0, 1.5);
            }
            if (il.PressedKeys.Contains(Keys.F))
            {
                tco2.Offset(-1.5, 0);
            }
            if (il.PressedKeys.Contains(Keys.H))
            {
                tco2.Offset(1.5, 0);
            }
            if (il.PressedKeys.Contains(Keys.T))
            {
                tco2.Offset(0, -1.5);
            }
            if (il.PressedKeys.Contains(Keys.G))
            {
                tco2.Offset(0, 1.5);
            }
            if (il.PressedKeys.Contains(Keys.Q))
            {
                tco3.Rotate(Coordinate.GetCenter(tco3.Boundary), -Math.PI / 90);
            }
            if (il.PressedKeys.Contains(Keys.E))
            {
                tco3.Rotate(Coordinate.GetCenter(tco3.Boundary), Math.PI / 90);
            }
            if (il.PressedKeys.Contains(Keys.R))
            {
                tco2.Rotate(Coordinate.Zero, -Math.PI / 90);
            }
            if (il.PressedKeys.Contains(Keys.Z))
            {
                tco2.Rotate(Coordinate.GetCenter(tco2.Boundary), Math.PI / 90);
            }

            this.collisionDBPanel.Refresh();
        }

        private void collisionDBPanel_Paint(object sender, PaintEventArgs e)
        {
            try
            {
                e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                e.Graphics.FillRectangle(Brushes.White, 0, 0, collisionDBPanel.Width, collisionDBPanel.Height);
                for (int i = 0; i < tco1.Boundary.Length - 1; i++)
                {
                    e.Graphics.DrawLine(Pens.Blue, tco1.Boundary[i].Point, tco1.Boundary[i + 1].Point);
                }
                e.Graphics.DrawLine(Pens.Blue, tco1.Boundary[tco1.Boundary.Length - 1].Point, tco1.Boundary[0].Point);

                for (int i = 0; i < tco2.Boundary.Length - 1; i++)
                {
                    e.Graphics.DrawLine(Pens.DarkGreen, tco2.Boundary[i].Point, tco2.Boundary[i + 1].Point);
                }
                e.Graphics.DrawLine(Pens.DarkGreen, tco2.Boundary[tco2.Boundary.Length - 1].Point, tco2.Boundary[0].Point);

                for (int i = 0; i < tco3.Boundary.Length - 1; i++)
                {
                    e.Graphics.DrawLine(Pens.DarkGreen, tco3.Boundary[i].Point, tco3.Boundary[i + 1].Point);
                }
                e.Graphics.DrawLine(Pens.DarkGreen, tco3.Boundary[tco3.Boundary.Length - 1].Point, tco3.Boundary[0].Point);


                for (int i = 0; i < cd.CollisionLocations.Length; i++)
                {
                    e.Graphics.DrawEllipse(Pens.Red, cd.CollisionLocations[i].Point.X - 4, cd.CollisionLocations[i].Point.Y - 4, 8, 8);
                }
            }
            catch { }
        }

    }
}
